package com.lanchetech.merchant.aop;


import com.alibaba.fastjson.JSON;
import com.lanchetech.common.util.IpUtil;
import com.lanchetech.dao.MerchantSysLogMapper;
import com.lanchetech.entity.MerchantSysLog;
import com.lanchetech.entity.SysLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Date;

/**
 * 系统日志：切面处理类
 */
@Aspect
@Component
public class SysLogAspect {

    @Autowired
    MerchantSysLogMapper merchantSysLogMapper;

    //定义切点 @Pointcut
    //在注解的位置切入代码
    @Pointcut("@annotation( com.lanchetech.merchant.aop.OperatorLog)")
    public void logPointCut() {
    }

    //切面 配置通知
    @AfterReturning("logPointCut()")
    public void saveSysLog(JoinPoint joinPoint) {
        //保存日志
        MerchantSysLog sysLog = new MerchantSysLog();

        //从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();

        //获取操作
        OperatorLog myLog = method.getAnnotation(OperatorLog.class);
        if (myLog != null) {
            String value = myLog.value();
            sysLog.setOperation(value);//保存获取的操作
        }

        //获取请求的类名
        String className = joinPoint.getTarget().getClass().getName();
        //获取请求的方法名
        String methodName = method.getName();
        sysLog.setMethod(className + "." + methodName);

        //请求的参数
        Object[] args = joinPoint.getArgs();
        //将参数所在的数组转换成json
        String params = "";
        // 过滤文件类型的参数。即上传文件时，要过滤，而下载文件时不需要。
        if (!"batchExpressGoodsOrder".equals(methodName)) {
            params = JSON.toJSONString(args);
        }
        if (params.length() > 255) {
            params = params.substring(0, 250);
        }
        sysLog.setParams(params);

        sysLog.setCreatedAt(new Date());
        //获取用户名
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (!(authentication instanceof AnonymousAuthenticationToken)) {
            sysLog.setUsername(authentication.getName());
        }
        //获取用户ip地址
        HttpServletRequest req = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
                .getRequest();
        sysLog.setIp(IpUtil.getIpAddr(req));
        //调用service保存SysLog实体类到数据库
        merchantSysLogMapper.insert(sysLog);
    }

}
